<template>
	<view class="address-form">
		<form @submit="formSubmit" @reset="formReset">
			<view class="bg-white p-0-30 f30">
				<view class="d-s-c">
					<text class="key-name">收货人</text>
					<input style="box-sizing: content-box;" class="ml20 flex-1 p-30-0" name="name" type="text" v-model="address.name" placeholder-class="grary" placeholder="请输入收货人姓名" />
				</view>
				<view class="d-s-c">
					<text class="key-name">联系方式</text>
					<input style="box-sizing: content-box;" class="ml20 flex-1 p-30-0" name="phone" type="text" v-model="address.phone" placeholder-class="grary" placeholder="请输入收货人手机号" />
				</view>
				<view class="d-s-c">
					<text class="key-name">所在地区</text>
					<view class="input-box flex-1">
						<input
						style="box-sizing: content-box;"
							class="ml20 flex-1 p-30-0"
							type="text"
							value=""
							placeholder-class="grary"
							placeholder=""
							v-model="selectCity"
							disabled="true"
							@click="showMulLinkageThreePicker"
						/>
					</view>
				</view>
				<view class="d-s-c">
					<text class="key-name">详细地址</text>
					<textarea style="box-sizing: content-box;" class="ml20 flex-1 p-30-0 lh150" name="detail" :auto-height="true" v-model="address.detail" placeholder="请输入街道小区楼牌号等"></textarea>
				</view>
			</view>
			<view class="p30"><button type="primary" form-type="submit" class="btn-paotui f30 mt30">确认</button></view>
		</form>
		<mpvue-city-picker ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
export default {
	components: {
		mpvueCityPicker
	},
	data() {
		return {
			cityPickerValueDefault: [0, 0, 0],
			selectCity: '选择省,市,区',
			province_id: 0,
			city_id: 0,
			region_id: 0,
			/*地址id*/
			address_id: 0,
			/*地址数据*/
			address: {},
			/*地区*/
			region: {}
		};
	},
	onLoad(e) {
		this.address_id = e.address_id;
	},
	mounted() {
		/*获取地址数据*/
		this.getData();
	},
	methods: {
		/*获取数据*/
		getData() {
			let self = this;
			let address_id = self.address_id;
			self._get(
				'user.address/detail',
				{
					address_id: address_id
				},
				function(res) {
					self.address = res.data.detail;
					self.address_id = res.data.detail.address_id;
					self.province_id = res.data.detail.province_id;
					self.city_id = res.data.detail.city_id;
					self.region_id = res.data.detail.region_id;
					self.region = res.data.region;
					let add = '';
					var b = self.region.forEach(item => {
						add += item;
					});
					self.selectCity = add;
				}
			);
		},

		/*提交地址*/
		formSubmit: function(e) {
			let self = this;
			var formdata = e.detail.value;
			console.log(e.detail.value)
			formdata.province_id = self.province_id;
			formdata.city_id = self.city_id;
			formdata.region_id = self.region_id;
			formdata.address_id = self.address_id;
			formdata.region = self.region;
			
			if (formdata.name == '') {
				uni.showToast({
					title: '请输入收货人姓名',
					duration: 1000,
					icon: 'none'
				});
				return false;
			}

			if (formdata.phone == '') {
				uni.showToast({
					title: '请输入手机号码',
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			/*  防止香港用户手机号加不了
			let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
			if (!reg.test(formdata.phone)) {
				uni.showToast({
					title: '手机号码格式不正确',
					duration: 1000,
					icon: 'none'
				});
				return false;
			}*/

			if (formdata.province_id == 0 || formdata.city_id == 0 || formdata.region_id) {
				if (formdata.detail == '') {
					uni.showToast({
						title: '请选择完整省市区',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
			}

			self._post('user.address/edit', formdata, function(res) {
				self.showSuccess(res.msg, function() {
					// #ifndef H5
					uni.navigateBack();
					// #endif
					// #ifdef H5
					history.go(-1);
					// #endif
				});
			});
		},

		/*清空数据*/
		formReset: function(e) {
			console.log('清空数据');
		},

		/*三级联动选择*/
		showMulLinkageThreePicker() {
			this.$refs.mpvueCityPicker.show();
		},

		/*选择之后绑定*/
		onConfirm(e) {
			this.region=e.label.split(',');
			this.selectCity = e.label;
			this.province_id = e.cityCode[0];
			this.city_id = e.cityCode[1];
			this.region_id = e.cityCode[2];
		}
	}
};
</script>

<style>
.address-form .key-name {
	width: 200rpx;
}
.address-form .btn-red{
	height: 88rpx;
	line-height: 88rpx;
	border-radius: 44rpx;
	box-shadow: 0 8rpx 16rpx 0 rgba(226,35,26,.6);
}
</style>
